package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.ItemAppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价信息--控制层
 * Created by zhongjieyu on 2015/1/22.
 */
@Controller
@RequestMapping("/itemAppraise")
public class ItemAppraiseController {

    @Autowired
    private ItemAppraiseService itemAppraiseService;

    /**
     * 跳转到评价信息页面
     * @param request
     * @param contractId 合同ID
     * @param type 用户类型 0为艺术家 1为发包方
     * @return
     */
    @RequestMapping(value = "/appraise", method = RequestMethod.GET)
    public String appraise(HttpServletRequest request,int contractId,int type){

        request.setAttribute("contractId",contractId);
        request.setAttribute("type",type);

        try {
            Map itemAppraise = itemAppraiseService.findItemAppraiseByContractId(contractId,type);

            request.setAttribute("itemAppraise",itemAppraise);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "/appraise/appraise";
    }

    /**
     * 保存评价信息
     * @param request
     * @param param 参数集
     * @return
     */
    @RequestMapping(value = "/saveAppraise", method = RequestMethod.POST)
    public ResponseEntity<?> saveAppraise(HttpServletRequest request,@RequestParam Map param){

        //获取当前用户ID
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        //等于空表示未登录
        if(userInfo!= null){
            //当前用户默认为评价方
            param.put("appraiser",userInfo.getUserId());

            try {
                itemAppraiseService.addItemAppraise(param);
            } catch (Exception e) {
                e.printStackTrace();

                ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
                return responseEntity;
            }

        }



        ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);
        return responseEntity;

    }


    /**
     * 根据作品ID分页查询作品评价数据
     * @param rows
     * @param page
     * @param works_id
     * @return
     */
    @RequestMapping(value = "/findWorksAppraiseByWorksId", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findWorksAppraiseByWorksId(int rows,int page,String works_id) {


        PageInfo pageInfo = null;
        try {

            PageHelper.startPage(page, rows);

            Map<String,String> param = new HashMap<String, String>();

            param.put("works_id",works_id);


            List<Map<String,Object>> list =  itemAppraiseService.findWorksAppraiseByWorksId(param);

            pageInfo =  new PageInfo(list);
//            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
//            return responseEntity;


        } catch (Exception e) {

            e.printStackTrace();


//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }


        return pageInfo;

    }

    /**
     * 跳转到评价信息页面
     * @param request
     * @param works_id 作品ID
     * @return
     */
    @RequestMapping(value = "/worksAppraise", method = RequestMethod.GET)
    public String worksAppraise(HttpServletRequest request,String works_id){

        request.setAttribute("works_id",works_id);

        try {

            //获取当前用户ID
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
            Map<String, String> param = new HashMap<String, String>();

            //评价方
            param.put("appraiser",String.valueOf(userInfo.getUserId()));
            param.put("works_id",works_id);
            Map worksAppraise = itemAppraiseService.findWorksAppraiseByUserId(param);

            request.setAttribute("worksAppraise",worksAppraise);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "/appraise/worksAppraise";
    }

    /**
     * 保存作品评价信息
     * @param request
     * @param param 参数集
     * @return
     */
    @RequestMapping(value = "/addWorksAppraise", method = RequestMethod.POST)
    public ResponseEntity<?> addWorksAppraise(HttpServletRequest request,@RequestParam Map param){

        //获取当前用户ID
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        //等于空表示未登录
        if(userInfo!= null){
            //当前用户默认为评价方
            param.put("appraiser",userInfo.getUserId());

            try {
                itemAppraiseService.addWorksAppraise(param);
            } catch (Exception e) {
                e.printStackTrace();

                ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
                return responseEntity;
            }

        }



        ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);
        return responseEntity;

    }
}
